package org.rency.spring.cloud.boot.mvc.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

/**
 * ${todo}
 *
 * @author: Created by user_rcy@163.com on 2016/8/22.
 * @version: V1.0.0
 * @since JDK 1.8
 */
//@Order(1)
//@Configuration
//@EnableTransactionManagement
public class DataBaseConfiguration implements EnvironmentAware {

    private final Logger logger = LoggerFactory.getLogger(DataBaseConfiguration.class);

    private final String DATASOURCE_CONFIG_PREFIX = "restx.persist.ds.";
    private final String JDBC_CONFIG_TYPE = "type";
    private final String JDBC_CONFIG_LOCATION = "config";
    private final String JDBC_CONFIG_PROPERTIES_PREFIX = "prefix";



    private final String MYBATIS_CONFIG_PREFIX = "mybatis.";

    private final String MYBATIS_CONFIG_LOCATION = "config";

    private final String MYBATIS_CONFIG_TYPEALIASESPACKAGE = "typeAliasesPackage";

    private final String MYBATIS_CONFIG_MAPPERLOCATIONS = "mapperLocations";

    private final String MYBATIS_CONFIG_BASEPACKAGE = "basePackage";

    private final String SESSION_FACTORY_BEAN_NAME = "sqlSessionFactory";

    private RelaxedPropertyResolver dsPropertyResolver;

    private RelaxedPropertyResolver propertyResolver;

    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment,MYBATIS_CONFIG_PREFIX);
        this.dsPropertyResolver = new RelaxedPropertyResolver(environment,DATASOURCE_CONFIG_PREFIX);
    }

    /*@Bean
    @Primary
    @ConfigurationProperties(prefix = DATASOURCE_CONFIG_PREFIX)
    public DataSource dataSource() {
        logger.info("Setting DataSource");
        DataSource dataSource = new DruidDataSource();
        logger.debug("DataSource Properties:{}.",dataSource.toString());
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager txManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean(name = SESSION_FACTORY_BEAN_NAME)
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(propertyResolver.getProperty(MYBATIS_CONFIG_LOCATION)));
        sessionFactory.setTypeAliasesPackage(propertyResolver.getProperty(MYBATIS_CONFIG_TYPEALIASESPACKAGE));

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources(propertyResolver.getProperty(MYBATIS_CONFIG_MAPPERLOCATIONS)));

        return sessionFactory.getObject();
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() throws Exception {
        MapperScannerConfigurer scanner = new MapperScannerConfigurer();
        scanner.setBasePackage(propertyResolver.getProperty(MYBATIS_CONFIG_BASEPACKAGE));
        scanner.setSqlSessionFactoryBeanName(SESSION_FACTORY_BEAN_NAME);
        return scanner;
    }*/

}
